﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using DataEntity;
using Model;

namespace SWA_View
{
    public partial class TestData : Form
    {
        public ObservableCollection<ExperimentCombinationEntity> _Data = new ObservableCollection<ExperimentCombinationEntity>();
        private int _ProjectID = 0;
        private ExperimentType _Type;
        CommonModel model = null;
        public TestData(int EXEID, int ProjectID, ExperimentType type)
        {
            InitializeComponent();
            model = new CommonModel();
            foreach (var c in model.Query<ExperimentCombinationEntity>(" ID=" + EXEID)) 
            {
                _Data.Add(c);
                _ProjectID = ProjectID;
                _Type = type;
                _Data.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_Data_CollectionChanged);
                _Data_CollectionChanged(null, null);
            
            }
        }

        public TestData(ObservableCollection<ExperimentCombinationEntity> datas, int ProjectID, ExperimentType type)
        {
            InitializeComponent();
            _Data = datas;
            _ProjectID = ProjectID;
            _Type = type;
            string ExistIDs = 0 +"";
            if (datas.Count > 0)
            {               
                foreach (var d in datas)
                {
                    ExistIDs += " ," + d.ID;
                }
            }
            model = new CommonModel();
            foreach (var c in model.Query<ExperimentCombinationEntity>(" ProjectID=" + ProjectID + " and ExperimentType ='" + type + "' and ID not in ("+ ExistIDs+")"))
            {
                _Data.Add(c);
            }

            _Data.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_Data_CollectionChanged);
            _Data_CollectionChanged(null, null);
            
        }

        void _Data_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            ListViewItem lvi = null;
            listTestData.Items.Clear();
            foreach (var c in _Data)
            {
                lvi = new ListViewItem(c.DataCombination);
                lvi.Tag = c.ID;
                //lvi.SubItems.Add((x + 1) + "");
                lvi.SubItems.Add(((DateTime)c.CreateDate).ToShortDateString());
                lvi.SubItems.Add(c.IsIng);
                lvi.SubItems.Add(c.IsFinished);
                this.listTestData.Items.Add(lvi);
            }

        }


        public ObservableCollection<ExperimentCombinationEntity> result = new ObservableCollection<ExperimentCombinationEntity>();
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (model != null)
                model = null;
            this.Close();
        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            result.Clear();
            if (model == null)
                model = new CommonModel();
            foreach (ListViewItem item in listTestData.Items)
            {
                if (item.Checked == true)
                {
                    int index = listTestData.Items.IndexOf(item);
                    ExperimentCombinationEntity entity = _Data[index];
                    if (entity.ID <= 0)
                    {
                       model.Save<ExperimentCombinationEntity>(entity);                      
                    }
                    result.Add(entity);

                }
            }
            if (result.Count <= 0)
            {
                MessageBox.Show("请选择实验项!");
                return;
            }
            if (model != null)
                model = null;
            this.DialogResult = DialogResult.OK;

            this.Close();
        }



        private void TestData_Load(object sender, EventArgs e)
        {

        }




        private void listTestData_ItemChecked(object sender, ItemCheckedEventArgs e)
        {

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

        }

        private void btnSelAll_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listTestData.Items)
            {
                item.Checked = true;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listTestData.Items)
            {
                item.Checked = false;
            }
        }
    }
}
